﻿using System.Collections.Generic;
using KillBill.Business.Entities;
using KillBill.Presentation.Common.WindowsForms.View;
using KillBill.Presentation.Modules.Products.IView;
using KillBill.Presentation.Modules.Products.Presenter;

namespace KillBill.Presentation.Modules.Products.View
{
    public partial class ProductListView : KillBillWinFormView, IProductList
    {
        public ProductListPresenter Presenter { get; set; }

        private List<Product> _productList;

        public ProductListView()
        {
            InitializeComponent();
            Presenter = new ProductListPresenter(this);
        }

        public List<Product> Products
        {
            set
            {
                var rowIndex = 0;
                DgvProducts.Rows.Clear();
                foreach (var product in value)
                {
                    DgvProducts.Rows.Add();
                    DgvProducts[0, rowIndex].Value = product.Provider.Name;
                    DgvProducts[1, rowIndex].Value = product.Name;
                    DgvProducts[2, rowIndex].Value = product.Description;
                    DgvProducts[3, rowIndex].Value = product.LastPrice;
                    DgvProducts[4, rowIndex].Value = product.Stock;
                    DgvProducts[5, rowIndex].Value = product.StockMin;
                    DgvProducts[6, rowIndex].Value = product.StockMax;
                    //DgvProducts[7, rowIndex].Value = (product.IVAApplied ? : );
                    //DgvProducts[8, rowIndex].Value = product.ICETax;
                    //DgvProducts[9, rowIndex].Value = (product.IsActive ? : );
                    rowIndex++;
                }
                _productList = value;
            }
        }

        private void BtnRefresh_Click(object sender, System.EventArgs e)
        {
            Presenter.Find(TxtSearch.Text);
        }

        private void TxtSearch_TextChanged(object sender, System.EventArgs e)
        {
            Presenter.Find(TxtSearch.Text);
        }

        private void BtnEdit_Click(object sender, System.EventArgs e)
        {
            var product = new Product(); //_productList
            var form = new ProductAddEditView(product);
            form.ShowDialog();
        }

        private void BtnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
